package viewmodel;

import items.BetItem;
import items.BetSideItem;

import java.util.HashMap;

import android.app.betmein.R;
import android.app.betmein.activity.DisplayBetActivity;
import android.app.betmein.view.BetManagmentJoinTabView;
import android.app.betmein.view.ViewCreatorHelper;
import android.content.Context;
import android.framework.binding.BindableList;
import android.framework.binding.Binding;
import android.framework.binding.ItemList;
import android.framework.binding.MethodRepresent;
import android.framework.paging.BaseViewModel;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import componentes.CommunicationManager;

public class BetManagmentJoinTabViewModel extends BaseViewModel<BetManagmentJoinTabView>{

	private ItemList<BetItem> bets;
	
	public BetManagmentJoinTabViewModel(Context context) {
		super(context);
		try {
			bets = new ItemList<BetItem>(context, R.layout.single_join_bet, ViewCreatorHelper.betListViewCreator(), new HashMap<Integer, MethodRepresent>(){
				{
					put(R.id.takeSideButton, new MethodRepresent(BetManagmentJoinTabViewModel.class.getDeclaredMethod("onSideItemClicked", BetItem.class, BetSideItem.class), BetManagmentJoinTabViewModel.this));
					put(R.id.beAJudge, new MethodRepresent(BetManagmentJoinTabViewModel.class.getDeclaredMethod("onJudgeButtonClicked", BetItem.class), BetManagmentJoinTabViewModel.this));
					put(R.id.reject, new MethodRepresent(BetManagmentJoinTabViewModel.class.getDeclaredMethod("onRejectButtonClicked", BetItem.class), BetManagmentJoinTabViewModel.this));
				}
			});
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void onSideItemClicked(BetItem item, BetSideItem betSideItem){
		CommunicationManager.getInstance().userChooseBetSide(item, betSideItem);
	}
	
	public void onJudgeButtonClicked(BetItem item){
		CommunicationManager.getInstance().setUserAsJudge(item);
	}
	
	public void onRejectButtonClicked(BetItem item){
		CommunicationManager.getInstance().userReject(item);
	}

	@Override
	public void onInit() {
		getView().bets.setAdapter(bets);
		
		BindableList<BetItem> joinedRequestBet = CommunicationManager.getInstance().getJoinedRequestBets();
		bets.addAll(joinedRequestBet);
		Binding.attached(joinedRequestBet, bets);
		
		getView().bets.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapter, View arg1, int pos,
					long arg3) {
				BetItem betItem = (BetItem)adapter.getItemAtPosition(pos);
				startActivity(DisplayBetActivity.class, betItem.getBet());
			}
		});
	}

}
